var ns;
Vue.component('tree-item', {
    template: '#item-template',
    props: {
        item: {}
    },
    data: function () {
        return {
            isOpen: true,
            selecttypeid: app.TypeID
        }
    },
    computed: {
        isFolder: function () {
            return this.item.children &&
                this.item.children.length
        }
    },
    methods: {
        toggle: function (item) {
            if (this.isFolder) {
                this.isOpen = !this.isOpen
            } else {
                this.choose_type(item);
            }
        },
        choose_type: function (item) {
            var that = this;
            app.TypeID = item.ID;
            app.TypeName = item.text;
            setTimeout(function () {
                app.is_type_list = false;
            }, 100)
        },
    }
})
var app = new Vue({
    el: '#app',
    data: {
        list: [],
        ServiceType: 1, //1-客服工单 2-投诉建议 3-品控管理 4-审批中心
        PublishType: 1, //1-派单 2-工单转移 3-工单协同 4-重派 5-审批 6-抄送
        source: '',//来源 chooseuser:选择负责人
        ID: 0,
        select_id_list: [],
        category_list: [],
        is_xietong: true,
        is_type_list: false,
        is_tag_list: false,
        is_category_list: false,
        typelist: [],
        TypeID: 0,
        TypeName: '请选择',
        taglist: [],
        TagID: 0,
        TagName: '请选择',
        categorylist: [],
        CategoryID: 0,
        CategoryName: '请选择',
        showchoose: false,
        showsearch: false,//遮罩层
        chooseList: [],
        form: {
            keywords: '',
        },
    },
    methods: {
        show_morehistory: function (item) {
            var that = this;
            item.isopen = !item.isopen;
        },
        doHideSearch: function () {
            var that = this;
            that.showchoose = false;
            that.showsearch = false;
        },
        showChooseAll: function () {
            var that = this;
            that.showchoose = !that.showchoose;
            that.showsearch = !that.showsearch;//底部阴影

        },
        doChoosUser: function (item) {
            var that = this;
            //单选
            if (that.source == 'chooseuser') {
                that.chooseList = [];
                that.chooseList.push(item);
            }
            //多选
            else {
                item.isselected = !item.isselected;

                if (item.isselected) {
                    that.chooseList.push(item);
                }
                else {
                    const index = that.chooseList.findIndex(p => p.ID === item.ID && p.TypeID === item.TypeID);
                    that.chooseList.splice(index, 1)
                }

                const new_list = [...new Set(that.chooseList)];// 去重
                that.chooseList = new_list;
            }
        },
        get_data: function () {
            var that = this;
            var actionValue = "customerservicesenduser";
            if (that.ServiceType == 3) {
                actionValue = "getorgnizationlist";
            }
            if (that.ServiceType == 4) {
                actionValue = "getuserlist";
            }
            var options = {
                ID: that.ID,
                action: actionValue,
                ServiceType: that.ServiceType,
                keyWords: that.form.keywords,
            };
            ns.post(options, function (succeed, data, err) {
                if (that.ServiceType == 1 || that.ServiceType == 2) {
                    if (data != null) {
                        if (data.status) {
                            that.list = data.userList;

                        } else {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }
                }
                if (that.ServiceType == 4) {
                    if (data.status) {
                        that.list = data.data;
                    }
                }
                if (that.ServiceType == 3) {
                    if (succeed) {
                        that.list = data.list;
                        that.typelist = data.typelist;
                        that.TypeID = data.TypeID;
                        that.TypeName = data.TypeName;
                        that.taglist = data.taglist;
                        that.TagID = data.TagID;
                        that.TagName = data.TagName;
                        that.categorylist = data.categorylist;
                        that.CategoryID = data.CategoryID;
                        that.CategoryName = data.CategoryName;
                    } else {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }
            }, {
                toast: true
            });
        },
        clear_searchVal: function () {
            var that = this;
            document.getElementById("search-input").value = '';
        },
        do_search: function () {
            this.get_data();
        },
        do_select_publishtype: function (type) {
            this.PublishType = type;
        },
        choose_tag: function (item) {
            var that = this;
            that.TagID = item.ID;
            that.TagName = item.Name;
            that.is_tag_list = false;
        },
        choose_category: function (item) {
            var that = this;
            that.CategoryID = item.ID;
            that.CategoryName = item.Name;
            that.is_category_list = false;
        },
        tel_phone: function (item) {
            api.call({
                type: 'tel_prompt',
                number: item.phone
            });
        },
        open_type_list: function () {
            var that = this;
            that.is_tag_list = false;
            that.is_category_list = false;
            that.is_type_list = !that.is_type_list;
        },
        open_tag_list: function () {
            var that = this;
            that.is_type_list = false;
            that.is_tag_list = !that.is_tag_list;
            that.is_category_list = false;
        },
        open_category_list: function () {
            var that = this;
            that.is_type_list = false;
            that.is_tag_list = false;
            that.is_category_list = !that.is_category_list;
        },
        do_choose: function (item, list) {
            item.isselected = !item.isselected;
        },
        set_select_items: function () {
            var that = this;
            that.select_id_list = [];
            that.category_list = [];
            for (var i = 0; i < that.chooseList.length; i++) {
                var item = that.chooseList[i];
                if (that.ServiceType != 3) {
                    that.select_id_list.push(item.userID);
                }
                if (that.ServiceType == 3) {
                    that.category_list.push(item);
                }

            }
        },
        do_save: function () {
            var that = this;
            that.set_select_items();
            if (that.select_id_list.length == 0) {
                api.toast({
                    msg: '请选择服务人员',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }

            var options = {};
            options.action = "dopublicservice";
            options.ID = that.ID;
            options.PublishType = that.PublishType; // 1派单 2转交 3协同 4重派
            options.ServiceType = that.ServiceType;  //1-客服工单 2-投诉建议 3-品控管理
            options.TypeID = that.TypeID;//可以不要，已经取消
            options.TagID = that.TagID;//可以不要，已经取消
            options.AcceptUserIDs = JSON.stringify(that.select_id_list);
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        api.toast({
                            msg: '操作成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    else {
                        api.toast({
                            msg: data.Msg == undefined ? '操作失败' : data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                    api.sendEvent({
                        name: 'updata_task_con',
                    });
                } else if (err) {
                    api.toast({
                        msg: err == undefined ? '接口失败' : err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                setTimeout(function () {
                    api.closeWin();
                }, 2000)
            }, {
                toast: true,
                toastmsg: '提交中'
            });
            return;
        },
        toChooseUser: function () {
            var that = this;
            // 单选派单，重派
            if (that.ID > 0) {
                that.do_save();
            }
            // 新增 选择负责人
            else {
                api.sendEvent({
                    name: 'updata_choose_user',
                    extra: {
                        data: that.chooseList,
                    }
                });
                setTimeout(function () {
                    api.closeWin();
                }, 500)
            }
        },
        //整改任务重派单
        toRepostUser: function () {
            var that = this;
            that.set_select_items();
            var options = {};
            if(that.PublishType==2){
                options.action = "qulitycontrolchangeuser";
                options.ID = that.ID;
                options.UserID = that.category_list[0].ID;
                options.UserName = that.category_list[0].text;
            }else{
                options.action = "refreshpublishquality";
                options.ID = that.ID;
                options.AcceptUserID = that.category_list[0].ID;
                options.AcceptUserName = that.category_list[0].text;
            }
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    api.toast({
                        msg: '操作成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                    api.sendEvent({
                        name: 'updata_task_con',
                    });
                    api.sendEvent({
                        name: 'close_detail',
                    });
                    setTimeout(function () {
                        api.closeWin();
                    }, 500)

                } else {
                    api.toast({
                        msg: '操作失败',
                        duration: 2000,
                        location: 'bottom'
                    });
                }

            })
        },
        //工单回访-重新派单-返回选择人员
        do_save_hf:function(){
            var that = this;
            api.sendEvent({
                name: 'purchase_add',
                extra: {
                    data: that.chooseList,
                }
            });
            setTimeout(function () {
                api.closeWin();
            }, 500)
        }
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    ns.init_search();
    app.ID = api.pageParam.id || 0;
    app.PublishType = api.pageParam.type || 0;
    app.ServiceType = api.pageParam.ServiceType || 1;
    app.source = api.pageParam.source;
    app.is_xietong = api.pageParam.is_xietong || false;
    setTimeout(function () {
        app.get_data();
    }, 500)
};